﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microhard.Permission.Interface;
using Micrhard.FMCG.Server.Models;
using Microsoft.Practices.Unity;
using Micrhard.FMCG.Server.Interface;
using FluentValidation.Mvc;
using Microhard.Permission.MVCFilter;
using Microhard.Permission.MVCFilter.MVCFilter;
using Microhard.Core;
using Microhard.Core.DBUtility;

namespace Microhard.FMCG.WebUI.Areas.WrokPlan.Controllers
{
    public class CheckInController : MicroHardBaseController<CheckinModel>
    {
        private ICheckin _resp = null;
        [Dependency]
        public ICheckin Category
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        [MVCFilterAtrForWeixinMP]
        public ActionResult CheckInSucceed(string SubKACode)
        {
            ViewData["SubKACode"] = SubKACode;
            return View();
        }

        public override ActionResult Create(Guid? newPKID, ScopeResult scope)
        {
            ISubKA subKA = IoC.Resolve<ISubKA>();
            var kas = subKA.GetKAByWeixinOpenID(ViewData["WeixinName"].ToString());
            CheckinModel cm = new CheckinModel();
            cm.OperationStatus = Core.DBUtility.OperationStatus.Add;
            ViewData["MngSubKA"] = kas.Select(k => new SubKAJSONModel
            {
                Lat = k.Baidulat,
                Lng = k.Baidulng,
                Name = k.Name,
                SubID = k.SubID
            }).ToList();
            ViewData["fullUrl"] = this.HttpContext.Request.Url.OriginalString;
            return View(cm);
        }

        [MVCFilterAtrForReDirect(Order = 9, RedirectActionName = "CheckInSucceed", RedirectAreaName = "WrokPlan", RedirectControllerName = "CheckIn", ResultModelExchangeParmsModelKeys = "SubKACode")]
        public override ActionResult SaveCreate([CustomizeValidator(RuleSet = "Insert")]CheckinModel model, bool continueEditing, Core.DBUtility.ScopeResult scope)
        {
            ISubKA subKA = IoC.Resolve<ISubKA>();
            var kas = subKA.GetKAByWeixinOpenID(model.WeixinName);
            ViewData["MngSubKA"] = kas.Select(k => new SubKAJSONModel
            {
                Lat = k.Baidulat,
                Lng = k.Baidulng,
                Name = k.Name,
                SubID = k.SubID
            }).ToList();
            ViewData["fullUrl"] = this.HttpContext.Request.UrlReferrer.OriginalString;
            return base.SaveCreate(model, continueEditing, scope);
        }
    }
}
